/**
 * 
 */
package org.lookouster.mopg.bo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;

import org.lookouster.mopg.connector.Database;

/**
 * @author lkster
 *
 */
public class Payment {
	
	String paymentNumber, payerNumber;
	Bill paidBill;
	ServiceProvider provider;
	Date paymentDate;
	
	public Payment(String billNo, String pNumber, int providerId )
	{
		provider = new ServiceProvider(providerId);
		paidBill = Bill.getBillByNo(billNo, providerId);
		payerNumber = pNumber;
		paymentDate = Calendar.getInstance().getTime();
		paymentNumber = generateNewPaymentNumber(provider);
		
	}
	
	
	
	public static String generateNewPaymentNumber(ServiceProvider prov)
	{
		String lastNo;
		try {	
				lastNo = Database.retrieveData("select payment_no from payments where id = (select max(id) from payments where provider_id = '" + prov.getProviderId() + "')").getString("payment_no");
				return prov.getProviderName() + String.format("%06d", Integer.parseInt(lastNo.split(prov.getProviderName())[1]) + 1);
			} catch (SQLException e) {
			 //TODO Auto-generated catch block
			//e.printStackTrace();
			return prov.getProviderName() + "000001";
		}
	}
	
	
}
